package com.buyhouse.zhaimao.fragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.buyhouse.recyclerview.interfaces.ItemClickListener;
import com.buyhouse.zhaimao.BaseFragment;
import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.adapter.MyAdapter;
import com.buyhouse.zhaimao.model.ExpertListBean;
import com.buyhouse.zhaimao.model.HouseResouseBean;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class CommunityInfoFragment extends BaseFragment {
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options, headOptions;
	private LinearLayoutManager layoutManager;
	//横向滑动组件
	private RecyclerView recyclerView;
	//适配器
	private MyAdapter adapter;
	private HotHouseAdapter hadapter;
	//横向的专家
	private List<ExpertListBean> list = new ArrayList<ExpertListBean>();
	//纵向房源
	private List<HouseResouseBean> listv = new ArrayList<HouseResouseBean>();
	private ListView listview_houseResous;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	protected int getContentView() {
		return R.layout.fragment_communityinfo;
	}

	@Override
	protected void initFragment() {
		iniData();
		initView();
	}

	protected void initView() {
		listview_houseResous = (ListView) this.findViewById(R.id.listview_houseResous);
		recyclerView = (RecyclerView) this.findViewById(R.id.recyclerview);
		recyclerView.setHasFixedSize(true);
		layoutManager = new LinearLayoutManager(getActivity());
		layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		recyclerView.setLayoutManager(layoutManager);
		adapter = new MyAdapter(getActivity(), list);
		// 为Item具体实例点击3种事件
		adapter.setItemClickListener(new ItemClickListener() {

			@Override
			public void onItemSubViewClick(View view, int postion) {

			}

			@Override
			public void onItemClick(View view, int postion) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onItemLongClick(View view, int postion) {
				// TODO Auto-generated method stub

			}
		});
		recyclerView.setAdapter(adapter);
		hadapter= new HotHouseAdapter();
		listview_houseResous.setAdapter(hadapter);
	}

	protected void iniData() {
		for(int i = 0;i<=10;i++){
		ExpertListBean bean = new ExpertListBean();
		bean.setName("王茹"+i);
		list.add(bean);
		}
		for(int i = 0;i<=10;i++){
			HouseResouseBean bean = new HouseResouseBean();
			bean.setTitle(("biaoti"+i));
			listv.add(bean);
			}
	}

	
	class HotHouseAdapter extends BaseAdapter{

		LayoutInflater inflater;
		
		public HotHouseAdapter() {
			inflater = LayoutInflater.from(getActivity());
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder vh = null;
			if(null == convertView){
				convertView = inflater.inflate(R.layout.item_houseresouse, parent, false);
				
				vh = new ViewHolder();
				
				vh.imgUrl = (ImageView)convertView.findViewById(R.id.imgUrl);
				vh.title = (TextView)convertView.findViewById(R.id.title);
				vh.price = (TextView)convertView.findViewById(R.id.price);
				vh.bedroom_hall_toilet = (TextView)convertView.findViewById(R.id.bedroom_hall_toilet);
				vh.acreage = (TextView)convertView.findViewById(R.id.acreage);
				vh.commission = (TextView)convertView.findViewById(R.id.commission);
				
				//把ViewHolder当作“标记”保存到当前的选项视图实例中
				convertView.setTag(vh);
			}else{
				vh = (ViewHolder)convertView.getTag();
			}
			
			HouseResouseBean bean = listv.get(position);
			vh.title.setText(bean.getTitle());
			vh.price.setText(bean.getPrice());
			vh.title.setText(bean.getBedroom()+"室"+bean.getHall()+"厅"+bean.getToilet()+"卫");
			vh.acreage.setText(bean.getAcreage());
			vh.commission.setText(bean.getCommission()+"元佣金");
			
			return convertView;
		}
		
	}
	private class ViewHolder{
		ImageView imgUrl;
		TextView title,price,bedroom_hall_toilet,acreage,commission;
		Button favorite;
	}
	private void initImgLoader(Context context) {
		imageLoader
				.init(new ImageLoaderConfiguration.Builder(context)
						.threadPriority(Thread.NORM_PRIORITY - 2)
						.threadPoolSize(3)
						.denyCacheImageMultipleSizesInMemory()
						.memoryCache(new WeakMemoryCache())
						.diskCacheFileNameGenerator(new Md5FileNameGenerator())
						.tasksProcessingOrder(QueueProcessingType.LIFO)
						.denyCacheImageMultipleSizesInMemory().writeDebugLogs()
						.build());
		options = new DisplayImageOptions.Builder()
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在内存中
				.imageScaleType(ImageScaleType.EXACTLY)
				// 图像将完全按比例缩小的目标大小
				.cacheOnDisk(true)
				// 设置下载的图片是否缓存在SD卡中
				.showImageForEmptyUri(R.drawable.ic_home)
				.showImageOnFail(R.drawable.ic_home)
				.showImageOnLoading(R.drawable.ic_home)
				.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
				.bitmapConfig(Bitmap.Config.RGB_565).build();
		


	}
	
}